<?php 
/**
* 注册校外用户
*/
class Register extends CI_Controller
{
    
    function __construct()
    {
        parent::__construct();

    }

    function student_register()
    {
        //表单验证
        if($this->form_validation->run('register/student_register'))
        {
          $this->load->model('user_model');
          $this->load->model('student_model');
          $this->load->model('user_model');
          $this->load->model('major_model');

          //获取参数          
          $username = $this->input->post('username');
          $password = $this->input->post('password');          
          $name = $this->input->post('name'); 
          $student_id = $this->input->post('student_id');
          $school_id = $this->input->post('school_id');
          
          $exist=$this->student_model->student_id_exist($student_id,$school_id);
          if($exist)
            {
              $this->session->set_flashdata('message','该学校已存在这个学号的学生，注册失败');
              redirect('register/student_register');
            }
          $major=$this->major_model->get_one_major_by_school_id($school_id);
          var_dump($major);
          if(!$major){
              $this->session->set_flashdata('message','该学校没有默认专业，注册失败');
              redirect('register/student_register');
          }
          //存入用户表
          $user_item = array(
              'username' => $username ,
              'password' => md5($password) ,
              'role_id' => 3 ,
            );  
          if($this->user_model->add($user_item))
          {
            $user_id=$this->db->insert_id();
            //存入学生表
            $student_item = array(
               'user_id' => $user_id ,
               'name' => $name ,                              
               'student_id' => $student_id,
               'major_id' => $major['id'] ,
               'type' => 1 ,
               'school_id' => $school_id ,
            );
            if($this->student_model->add($student_item))
            {
              $this->session->set_flashdata('message','注册成功');
              redirect('home/index');
            } 
            else
            {
              $this->user_model->delete($user_id);
              $this->session->set_flashdata('message','注册失败');
              redirect('register/student_register');
            }
          }
          else
          {
            $this->session->set_flashdata('message','注册失败');
            redirect('register/student_register');
          }
        }
        else
        {
          $this->load->model('school_model');
          $schools = $this->school_model->get_all();
          $data = array();
          $data['title']='数字资源系统';
          $data['login_state']="string";
          $data['message']= "";
          $data['title']='学生注册';
          $data['schools']=$schools;   
          $this->load->view('register/student_register',$data);
        }
    }
    function teacher_register()
    {
        //表单验证
        if($this->form_validation->run('register/teacher_register'))
        {
          $this->load->model('user_model');
          $this->load->model('teacher_model');
          $this->load->model('user_model');
          $this->load->model('major_model');

          //获取参数
          $school_id = $this->input->post('school_id');
          $username = $this->input->post('username');
          $teacher_number = $username;
          $name = $this->input->post('name');
          $password = $this->input->post('password');
          $exist=$this->teacher_model->teacher_number_exist($teacher_number,$school_id);
          if($exist)
            {
              $this->session->set_flashdata('message','该学校已存在这个用户名的教师，注册失败');
              redirect('register/teacher_register');
            }
          $major=$this->major_model->get_one_major_by_school_id($school_id);
          var_dump($major);
          if(!$major){
              $this->session->set_flashdata('message','该学校没有默认专业，注册失败');
              redirect('register/teacher_register');
          }
          //存入用户表
          $user_item = array(
              'username' => $username ,
              'password' => md5($password) ,
              'role_id' => 2 ,
            );  
          if($this->user_model->add($user_item))
          {
            $user_id=$this->db->insert_id();
            //存入教师表
            $teacher_item = array(
               'user_id' => $user_id ,               
               'teacher_number' => $teacher_number,
               'name' => $name ,
               'major_id' => $major['id'] ,
               'school_id' => $school_id ,
               'pass' => 0,
            );
            if($this->teacher_model->add($teacher_item))
            {
              $this->session->set_flashdata('message','注册成功，请等待审核');
              redirect('home/index');
            } 
            else
            {
              $this->user_model->delete($user_id);
              $this->session->set_flashdata('message','注册失败');
              redirect('register/teacher_register');
            }
          }
          else
          {
            $this->session->set_flashdata('message','注册失败');
            redirect('register/teacher_register');
          }
        }
        else
        {
          $this->load->model('school_model');
          $schools = $this->school_model->get_all();
          $data = array();
          $data['title']='数字资源系统';
          $data['login_state']="string";
          $data['message']= "";
          $data['title']='教师注册';
          $data['schools']=$schools;   
          $this->load->view('register/teacher_register',$data);
        }
    }
  /**
   * 异步jquery验证
   * @return [type] [description]
   */
  function check_username_exist()
  {
    $username=$this->input->post('username');
    $this->load->model('user_model');
    if($this->user_model->username_exist($username))
      echo json_encode(FALSE);
    else 
      echo json_encode(TRUE);
  }
   /**
    * CI的表单回调函数验证
    * @param  [type] $str [description]
    * @return [type]      [description]
    */
   function username_check($username)
   {
      $this->load->model('user_model');
      if($this->user_model->username_exist($username))
         return FALSE;
      else 
         return TRUE;
   }
   /**
   * 异步jquery验证
   * @return [type] [description]
   */
  function check_teacher_number_exist()
  {
    $teacher_number=$this->input->post('teacher_number');
    $school_id=$this->input->post('school_id');
    $this->load->model('teacher_model');
    if($this->teacher_model->teacher_number_exist($teacher_number,$school_id))
      echo json_encode(FALSE);
    else 
      echo json_encode(TRUE);
  }
   /**
    * CI的表单回调函数验证
    * @param  [type] $str [description]
    * @return [type]      [description]
    */
   function teacher_number_check($teacher_number,$school_id)
   {
      $this->load->model('teacher_model');
      if($this->teacher_model->teacher_number_exist($teacher_number,$school_id))
         return FALSE;
      else 
         return TRUE;
   }
   /**
   * 异步jquery验证
   * @return [type] [description]
   */
  function check_student_id_exist()
  {
    $student_id=$this->input->post('student_id');
    $school_id=$this->input->post('school_id');
    $this->load->model('student_model');
    if($this->student_model->student_id_exist($student_id,$school_id))
      echo json_encode(FALSE);
    else 
      echo json_encode(TRUE);
  }
   /**
    * CI的表单回调函数验证
    * @param  [type] $str [description]
    * @return [type]      [description]
    */
   function student_id_check($student_id,$school_id)
   {
      $this->load->model('student_model');
      if($this->student_model->student_id_exist($student_id,$school_id))
         return FALSE;
      else 
         return TRUE;
   }
}